import nattyFetch from 'natty-fetch';
import Utils from "../utils/index";

const {API_URL, MOCK_URL} = require(`./../config/${ENV}.json`);
const context = nattyFetch.context({
  mockUrlPrefix: MOCK_URL,
  urlPrefix: API_URL,
  mock: false,
  withCredentials: false,
  traditional: true,
  data: {
    _wx_token_: '',
  },
  timeout: 15000,
  // 请按照需要开启
  fit: function (response) {
    let ret = {
      success: response.success
    };
    if (ret.success) {
      ret.content = response.data;
    } else {
      ret.error = {
        message: response.msg
      }
    }
    return ret;
  }
});

context.create(
  'WechatAPI', {

    //获取配置
    auth: {
      url: 'wechat/api/sign',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
    },

    //上传图片
    uploadMediaTwo: {
      mock: false,
      url: 'file/uploadfiletwo',

      method: 'POST',
      fit: function (response) {
        let ret = {
          success: true
        };
        if (ret.success) {
          ret.content = response;
        }
        else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    //上传图片
    uploadMedia: {
      mock: false,
      url: 'file/uploadfile',

      method: 'POST',
      fit: function (response) {
        let ret = {
          success: true
        };
        if (ret.success) {
          ret.content = response;
        }
        else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    //删除图片
    delFileMedia: {
      mock: false,
      url: 'file/delfile',
    },

    // 获取图片
    getFileImages: {
      url: 'file/getfiles',
      method: 'GET',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
    },

    //更新用户信息
    upProprietor: {
      mock: false,
      url: 'mp/upProprietor',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      fit: function (response) {
        let ret = {
          success: true
        };
        if (ret.success) {
          ret.content = response;
        }
        else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    updIntro: {
      url: 'yz/updIntro',
      method: 'POST',
      timeout: 30000
    },

    //获取用户信息
    getUser: {
      mock: false,
      url: 'wechat/get/user',
      method: 'GET',
      storage: true
    },

    getLocalUser: {
      mock: false,
      url: 'wechat/user/local',
      method: 'GET'
    },

    userIsBinding: {
      url: 'wechat/user/isBinding',
      method: 'GET',
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '已绑定';
        } else {
          ret.error = {
            message: response
          }
        }

        return ret;
      }
    },

    //验证用户信息
    checkUserCertNum: {
      url: 'yz/checkCert',
      method: 'GET'
    },

    //身份证号码验证业主信息
    checkUserCard: {
      url: 'yz/checkCard',
      method: 'GET'
    },

    //用户ID获取业主
    loadDoorplateById: {
      url: 'mp/loadDoorplateById',
      method: 'GET'
    },

    getInfoByOpenId: {
      url: 'mp/getinfoByOpenId',
      method: 'GET'
    },

    getDoorGroupCourt: {
      url: 'mp/getDoorGroupCourt',
      method: 'GET'
    },

    //用户ID获取绑定小区
    loadDoorBind: {
      url: 'mp/loadDoorBind',
      method: 'GET'
    },

    //用户ID获取小区
    loadCourtById: {
      url: 'mp/loadCourtById',
      method: 'GET'
    },

    // 获取所有小区
    getCourtAll: {
      url: 'xq/findAllPage',
      method: 'GET'
    },

    getCourtById: {
      url: 'xq/findOne',
      method: 'GET'
    },

    //用户电话号码获取小区
    findCourtByPhone: {
      url: 'mp/findPhonenByCourt',
      method: 'GET'
    },

    //小区ID获取当前任务
    findRenWuActive: {
      url: 'rw/findRenWuActive',
      method: 'GET'
    },

    //绑定业主
    bindDoor: {
      url: 'mp/doorbind',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response === 'success'
        };
        if (ret.success) {
          ret.content = '绑定成功';
        } else {
          ret.error = {
            message: response
          }
        }

        return ret;
      }
    },

    //参与
    bindCastok: {
      url: 'mp/doorCastok',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      }
    },

    //拒绝
    bindCastno: {
      url: 'mp/doorCastno',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      /*// 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '请求成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }*/
    },

    getProprietorByOpenId: {
      url: 'mp/getProprietorByOpenId',
      method: 'GET'
    },

    getProprietorById: {
      url: 'yz/getProprietorById',
      method: 'GET'
    },

    //绑定用户信息
    binding: {
      url: 'wechat/user/binding',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
    },

    // 獲取投票列表()
    getVoteListByProprietorId: {
      url: 'fatp/findPlanByProprietor',
      method: 'GET'
    },

    // 獲取投票方案信息詳情
    getVoteById: {
      url: 'fatp/findPlanInProcess',
      method: 'GET'
    },

    // 获取方案下的所有小区
    getDistrictByVoteId: {
      url: 'xq/findSelectedCourt',
      method: 'GET',
      storage: false
    },

    // 获取方案图片
    getPlanImages: {
      url: 'xq/img/getFiles',
      method: 'GET'
    },

    // 获取方案问卷调查
    getQuestion: {
      url: 'wjxx/findAllOptionByPlan',
      method: 'GET'
    },

    // 提交问卷调查
    doVote: {
      url: 'vote/addvoteInfo',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '投票成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    // 根据业主id，方案id，小区id，查询投票结果
    getVoteResult: {
      url: 'vote/findOneByCourtAndPerson',
      method: 'GET'
    },

    getPhoneNumberByCertNum: {
      url: 'yz/phone',
      method: 'GET',
      fit: function (response) {
        let ret = {};
        if (response.hasOwnProperty('success')) {
          ret = {
            success: response.success
          };
          if (ret.success) {
            ret.content = response.data;
          } else {
            ret.error = {
              message: response.msg
            }
          }
        } else {
          ret = {
            success: true,
            content: response
          };
        }
        return ret;
      }
    },

    sendVCodeMessage: {
      url: 'wechat/message/vcode/send',
      method: 'GET'
    },

    register: {
      url: 'yz/register',
      method: 'POST',
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = response.data;
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    }

  }
);

context.create(
  'ywh', {

    // 获取候选人列表
    getCandidates: {
      url: 'rw/findBuddySignUp',
      method: 'GET'
    },

    // 获取用户所在小区
    getXiaoqu: {
      url: 'mp/loadCourtById',
      method: 'GET'
    },

    // 获取业委会投票任务
    getYwhTp: {
      url: 'rw/loadCourtActiveTask',
      method: 'GET'
    },
    getYwhTp01: {
      url: 'rw/activeReWu',
      method: 'GET'
    },
    getYwhTp02: {
      url: 'rw/activeReWu/code',
      method: 'GET'
    },
    isVoted: {
      url: 'mp/ywh/isVoted',
      method: 'GET'
    },

    // 业委会投票
    ywhTp: {
      url: 'mp/castunitadd',
      method: 'POST',
      header: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      // 请按照需要开启
      fit: function (response) {
        let ret = {
          success: response.success
        };
        if (ret.success) {
          ret.content = '投票成功';
        } else {
          ret.error = {
            message: response.msg
          }
        }
        return ret;
      }
    },

    // 业委会投票2
    ywhVote: {
      url: 'mp/ywh/vote',
      method: 'POST'
    },

    wyJobs: {
      url: 'ywh/wyDuty',
      method: 'GET'
    },

    memberByRole: {
      url: 'ywhBind/member/role',
      method: 'GET'
    },

    // 除主任以外的成员
    memberByRoleButZr: {
      url: 'ywhBind/member/role/but/zr',
      method: 'GET'
    },

    wyJobById: {
      url: 'ywhBind/member/p/role',
      method: 'GET'
    },

    roleAppoint: {
      url: 'ywhBind/member/appoint',
      method: 'POST'
    },

    xjRwStatus: {
      url: 'rw/status',
      method: 'GET'
    },

    doorById: {
      url: 'mp/findOne',
      method: 'GET'
    },

    hasRole: {
      url: 'ywhBind/has/role',
      method: 'GET'
    },

    getRoleById: {
      url: 'ywh/findOne',
      method: 'GET'
    },

    ywhVoteRole: {
      url: 'mp/ywh/vote/role',
      method: 'GET'
    }
  }
);

context.create(
  'vote', {

    getSubjectById: {
      url: 'vote/subject/id',
      method: 'GET'
    },

    allSubject: {
      url: 'vote/subject/all',
      method: 'GET'
    },

    allSubjectByCreator: {
      url: 'vote/subject/all/by/creator',
      method: 'GET'
    },

    allSubjectByTitle: {
      url: 'vote/subject/all/by/title',
      method: 'GET'
    },

    allSubjectMyVoted: {
      url: 'vote/subject/all/my/voted',
      method: 'GET'
    },

    allQuestionBySubjectId: {
      url: 'vote/question/all',
      method: 'GET'
    },

    allOptionByQuestionId: {
      url: 'vote/option/all',
      method: 'GET'
    },

    saveSubject: {
      url: 'vote/subject/save',
      method: 'POST'
    },

    delQuestionById: {
      url: 'vote/question/del/id',
      method: 'POST'
    },

    questionnaire: {
      url: 'vote/subject/questionnaire',
      method: 'GET'
    },

    answerByQuestionId: {
      url: 'vote/answer/all/by/question',
      method: 'GET'
    },

    answerBySubjectAndQuestionAndOptionId: {
      url: 'vote/answer/subject/question/option/id',
      method: 'GET'
    },

    saveAnswer: {
      url: 'vote/answer/save',
      method: 'POST'
    },

    isVoted: {
      url: 'vote/answer/is/voted',
      method: 'GET'
    },

    delOptionById: {
      url: 'vote/option/del/id',
      method: 'POST'
    },

    voteCount: {
      url: 'vote/answer/vote/count',
      method: 'GET'
    },

    voteCountOption: {
      url: 'vote/answer/vote/count/option',
      method: 'GET'
    }

  }
);

context.create(
  'DoorAuth', {
    getProprietorByIC: {
      url: 'yz/checkCard',
      method: 'GET'
    },

    findDoorInfoById: {
      url: 'mp/findDoorInfoById',
      method: 'GET'
    },

    auth: {
      url: 'door/auth/save',
      method: 'POST'
    },

    isAuth: {
      url: 'door/auth/isauth',
      method: 'GET'
    },

    authInfo: {
      url: 'door/auth/info',
      method: 'GET'
    },

    getAuth: {
      url: 'door/auth/findOneByDoorId',
      method: 'GET'
    },

    // 撤销授权
    reAuth: {
      url: 'door/auth/del',
      method: 'POST'
    }
  }
);

context.create(
  'my', {
    //
    isCastOk: {
      url: 'mp/isCast',
      method: 'GET'
    }
  }
);

export default context.api;
